package com.auto.api.core.type.classtomap;

import com.auto.api.core.constants.MappingConstants;
import com.auto.api.core.entity.ClassToMapContext;
import com.auto.api.core.entity.ProcessProp;
import com.auto.api.core.entity.PropertyContext;
import com.auto.api.core.type.TypeFactory;
import java.beans.PropertyDescriptor;

/**
 * @program:
 * @description: 类型转换接口
 * @author: liangshf
 * @create: 2020-09-24
 */
public class ClassToColumnTypeMapHandler extends AbstractClassToMapHandler {


    public static final String STR = "&";
    public static final String COMMON = ",";

    @Override
    public ProcessProp<ClassToMapContext> getProcessProp(ClassToMapContext context, PropertyContext propertyContext) {
        PropertyDescriptor prop = propertyContext.getProp();
        String propertyName = prop.getName();
        String attrKey = propertyName + MappingConstants.LINE + context.getParentPropertyName() + propertyName;
        StringBuilder propTypeValue = new StringBuilder();
        propTypeValue.append(prop.getPropertyType().getSimpleName());
        return new ProcessProp(propertyContext.getJudgePropType(), context, propertyContext, attrKey, propTypeValue);
    }

    @Override
    public void processProp(ProcessProp<ClassToMapContext> processProp) {
        PropertyDescriptor prop = processProp.getPropertyContext().getProp();
        processProp.getPropTypeValue().append(COMMON);
        processProp.getPropTypeValue().append(processProp.getPropType());
        if (processProp.getFieldApiModel() != null) {
            processProp.getPropTypeValue().append(STR);
            processProp.getPropTypeValue().append(processProp.getFieldApiModel().value());
        }
        //多个继承泛型替换
        if (TypeFactory.isSimpleTypeHandler(processProp.getPropType()) || processProp.getPropertyContext().isCollectionType()) {
            processProp.getContext().getResult().put(processProp.getAttrKey(), processProp.getPropTypeValue().toString());
        } else {
            processProp.getContext().getResult()
                .putAll(getResult(new ClassToMapContext(processProp.getPropertyContext().isObjParse() ? processProp.getPropertyContext().getGenPropResType() : processProp.getPropertyContext().getPropResType(), processProp.getContext().getParentPropertyName() + prop.getName())));
        }
    }
}
